package source.events.action;

import source.model.*;
import source.model.type.ActionResult;
import source.model.type.GameObjectType;
import source.view.MainScreen;
import source.model.skill.*;

/**
 * Trap class is responsible for setting, detecting, removing traps with
 * the current unit.  If the current selection is not a unit, the Action fails.
 * @author Joe
 *
 */
public class Trap implements Action{

	private Unit unit;
	
	public Trap()
	{
		
	}
	public ActionResult execute( DataHandler dh ) {
		
		GameObject go = dh.getCurrentSelection();

		if(go.objectType() != GameObjectType.UNIT)
		{
			MainScreen.writeToConsole("Current selection is not an unit");
			return ActionResult.FAILED;
		}
		
		this.unit = (Unit)go;
		if(unit.getMissionFlag())
		{
			MainScreen.writeToConsole("Current selection is on a mission");
			return ActionResult.IMPOSSIBLE;
		}
		Skill aSkill = unit.getSkill("trap");
		if (aSkill == null){
			MainScreen.writeToConsole(unit.getName() + "s cannot lay traps!!");
			return ActionResult.IMPOSSIBLE;
		}
		
		SkillTrap trapper= (SkillTrap)aSkill;
		Position pos = dh.positionQuery(unit.objectID());
		dh.addGameObject(new TrapItem(trapper.trapStrength()), pos);
		MainScreen.writeToConsole("Trap laid!");
		
		return ActionResult.SUCCESS;
	}
}
